package btp.Calendars.Tests;

public class IdConverter {
	
	public static final long PRIME = 418195471;
	
	private static String num2str(long id){
		int n;
		String ans="";
		int times = 5;
		while (times--!=0)
		{
			n = (int) id%53;
			if(n==0)
				ans="0"+ans;
			else if(n<=26)
				ans=Character.toString((char) ('a'+(n-1)))+ans;
			else 
				ans=Character.toString((char) ('A' +(n-27)))+ans;
			id/=53;
		}
		return ans;
	}
	
	private static long invert(long id){
		long r1=PRIME, r2=id, r3;
		long x1=1, x2=0, y1=0, y2=1, x3, y3;
		long q=0;
		while(1==1){
			q = r1/r2;
			r3 = r1-r2*q;
			if(r3==0) break;
			x3 = x1 - x2*q;
			y3 = y1 - y2*q;
			
			r1 = r2;
			r2 = r3;
			x1 = x2;
			x2 = x3;
			y1 = y2;
			y2 = y3;
		}
		
		return (y2+PRIME)%PRIME;
	}
	private static long str2num(String str) {
		long ans = 0;
		for (int i=0; i<str.length(); i++)
		{
			ans*=53;
			if(str.charAt(i)>='a' && str.charAt(i)<='z')
				ans+=str.charAt(i)-'a'+1;
			else if(str.charAt(i)!='0')
				ans+=str.charAt(i)-'A'+27;
		}
		return ans;
	}
	
	public static String encode(long id){
		return num2str(invert(id));
	}
	
	public static long decode(String str){
		return invert(str2num(str));
	}
	
	

	public static void main(String[] args){
		System.out.println(decode(encode(50)));
		System.out.println(encode(1));
	}

}
